<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>文章列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
  <link rel="stylesheet" href="./css/reset.css" />
  <link rel="stylesheet" href="./css/iconfont.css" />
  <link rel="stylesheet" href="./css/main.css" />
  <link rel="stylesheet" href="./libs/layui/css/layui.css" />
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./js/jqconfig.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">文章列表</div>
    <div class="container-fluid common_con">
      <div class="row opt_btns">
        <div class="col-xs-6">
          <div class="form-inline">
            <input type="text" id="aname" class="form-control input-sm" placeholder="输入文章标题..." />
            <select id="selCategory" name="" class="form-control input-sm">
              <option value="">所有分类</option>
            </select>
            <select id="selStatus" name="" class="form-control input-sm">
              <option value="">所有状态</option>
              <option value="草稿">草稿</option>
              <option value="已发布">已发布</option>
            </select>
            <input id="btnSearch" type="button" value="筛选" class="btn btn-success btn-sm" />
            <input type="reset" value="重置" class="btn btn-danger btn-sm" onclick="rel()" />
          </div>
        </div>
        <div class="col-xs-6">
          <a href="./article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
        </div>
      </div>
      <!-- //文章列表 -->
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>标题</th>
            <th>作者</th>
            <th>分类</th>
            <th class="text-center">发表时间</th>
            <th class="text-center">状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>王积千造统最头</td>
            <td>杰克</td>
            <td>奇趣事</td>
            <td class="text-center">2017-06-08 07:08:46</td>
            <td class="text-center">已发布</td>
            <td class="text-center">
              <button class="btn btn-default btn-xs">编辑</button>

              <button class="btn btn-danger btn-xs delete">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 页脚 -->
      <div id="pager" class="row text-center">
        <ul class="pagination pagination-sm">
          <li class="page-item prev disabled">
            <a href="#" class="page-link">上一页</a>
          </li>
          <li class="page-item active">
            <a href="#" class="page-link">1</a>
          </li>
          <li class="page-item"><a href="#" class="page-link">2</a></li>
          <li class="page-item"><a href="#" class="page-link">3</a></li>
          <li class="page-item"><a href="#" class="page-link">4</a></li>
          <li class="page-item"><a href="#" class="page-link">5</a></li>
          <li class="page-item"><a href="#" class="page-link">6</a></li>
          <li class="page-item"><a href="#" class="page-link">7</a></li>
          <li class="page-item next">
            <a href="#" class="page-link">下一页</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <script src="./libs/layui/layui.js"></script>
  <script src="./libs/art-template/template-web.js"></script>

  <script id="tmp1" type="text/html">
      {{each data}}
      <tr>
        <td>{{$value.title}}</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
          <button
            class="btn btn-default btn-xs"
            onclick="location.href='./article_edit.html?id={{$value.id}}'"
          >
            编辑
          </button>

          <button
            class="btn btn-danger btn-xs delete"
            onclick="deleled('{{$value.id}}')"
          >
            删除
          </button>
        </td>
      </tr>
      {{/each}}
    </script>
  <script id="tmp2" type="text/html">
      {{each data}}
      <option value="{{$value.id}}">{{$value.name}}</option>

      {{/each}}
    </script>

  <script>
    // 2.分页
    var laypage, layer;
    layui.use(["laypage", "layer"], function () {
      layer = layui.layer;
      laypage = layui.laypage;
      getDataList();

      getCateList();
      btnSearchList();
    });

    let params = { page: 1, perpage: 6 };
    //重置按键功能
    function rel() {
      getDataList((params = { page: 1, perpage: 6 }));
    }
    //1.获取所有文章数据 展示到tbody中
    function getDataList() {
      $.ajax({
        type: "get",
        url: "/admin/article/query",
        data: params,
        success: (res) => {
          console.log(res);
          // console.log(res.data.data);
          // 展示到tbody中
          const htmlStr = template("tmp1", res.data);
          $("tbody").html(htmlStr);
          // 为了拿到服务器的数据总条数，应该在此处渲染分页控件
          laypage.render({
            elem: "pager", //注意，这里的 test1 是 ID，不用加 # 号
            count: res.data.totalCount, //数据总数，从服务端得到
            limit: params.perpage,
            curr: params.page,
            jump: function (obj, first) {
              // 如果 first 为true表示第一次渲染此控件，如果是undefined表示是用户点击了控件中的页码切换动作
              // console.log("first=>", first);
              // console.log(obj.curr, obj.limit);
              // 判断当前first为undefined的时候获取到当前的用户点击页码,同时发出新的数据请求获取最新页码对应的数据
              if (!first) {
                // 获取用户点击的页码
                let curr = obj.curr;
                // 修改params对象中的page属性的值为curr
                params.page = curr;

                getDataList();
              }
            },
          });
        },
      });
    }
    //3.获取文章分类数据
    function getCateList() {
      $.ajax({
        type: "get",
        url: "/admin/category/list",
        data: {},
        success: (res) => {
          // console.log(res);
          $("#selCategory").append(template("tmp2", res));
        },
      });
    }
    //4.筛选按钮
    function btnSearchList() {
      $("#btnSearch").click(function () {
        params.key = $("#aname").val().trim();
        params.type = $("#selCategory").val();
        params.state = $("#selStatus").val();
        console.log(params.key, params.type, params.state);
        getDataList();
      });
    }
    //5.删除按钮
    function deleled(id) {
      layer.confirm(
        "确认删除吗",
        { icon: 3, title: "删除确认" },
        function (index) {
          //数据请求
          $.ajax({
            type: "post",
            url: "/admin/article/delete",
            data: { id },
            success: (res) => {
              console.log(res);
              layer.msg(res.msg, { time: 500 });
              getDataList();
            },
          });
          layer.close(index);
        }
      );
    }
  </script>
</body>

</html>